//
//  WebSocketManager.m
//  test_reliable_lib
//
//  Created by Shine on 2019/6/5.
//  Copyright © 2019 uestc. All rights reserved.
//

#import "WebSocketManager.h"
#include "test_frame.h"

@implementation WebSocketManager

+ (instancetype)sharedInstance {
    static WebSocketManager * _sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[WebSocketManager alloc]init];
    });
    return _sharedInstance;
}

- (void)loginWebSocketBaseUrl:(NSString *)baseUrl Port:(NSString *)port userUid:(NSString *)uid UserToken:(NSString *)token {
    TestEvent * ev = new TestEvent();
    const char *url = baseUrl.UTF8String;
    uint16_t webPort = [port intValue];
    int ret = reliable_init(ev, sdk_log_cb, url, webPort);
    if (ret < 0) {
        getchar();
        return;
    }
    uint32_t userUID = [uid intValue];
    const char *userToken = token.UTF8String;
    ev->uid_ = userUID;
    uint8_t buf[1024] = { 0 };
    uint32_t len = 0;
    pack_login_req(userUID, buf, len);
    set_token_heartbeat_req(len, userToken, userUID, buf);
}

- (void)testEventOnMessage {
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"testEventOnMessage" object:self];
}
// 其他地方登陆了 强制退出
- (void)testForceLogout_Noti {
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"testForceLogout_Noti" object:self];
}

- (void)logOutConnect {
   reliable_destroy();
}

@end
